package com.zyht.common.xml;

import java.io.Writer;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.core.util.QuickWriter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.XppDriver;

public class XMLUtils {

	/**
	 * 扩展xstream，让xml节点增加CDATA标记
	 */
	public static final XStream getXStream(){
		return new XStream(new XppDriver() {

			public HierarchicalStreamWriter createWriter(Writer out){

				return new PrettyPrintWriter(out) {

					boolean CDATA = true;

					@SuppressWarnings("rawtypes")
					public void startNode(String name,Class clazz){

						super.startNode(name,clazz);
					}

					protected void writeText(QuickWriter writer,String text){

						if(CDATA){
							writer.write("<![CDATA[");
							writer.write(text);
							writer.write("]]>");
						} else{
							writer.write(text);
						}
					}
				};
			}
		});
	};
}
